package com.fendaren.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ServiceLogAspect {

    public static final Logger logger = LoggerFactory.getLogger(ServiceLogAspect.class);

    /**
     * 切面表达式
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("execution(* com.fendaren.service.impl..*.*(..))")
    public Object recordTimeLog(ProceedingJoinPoint joinPoint) throws Throwable {
        logger.info("====== 开始执行 {}.{}======",joinPoint.getTarget().getClass(),joinPoint.getSignature().getName());
        // 记录开始时间
        long begin = System.currentTimeMillis();
        // 执行目标service
        Object result = joinPoint.proceed();
        // 记录结束时间
        long end = System.currentTimeMillis();
        long takeTime = end - begin;

        if (takeTime>3000) {
            logger.error("====== 执行结束，耗时：{} 毫秒 ======",takeTime);
        } else if (takeTime>2000) {
            logger.warn("====== 执行结束，耗时：{} 毫秒 ======",takeTime);
        } else {
            logger.info("====== 执行结束，耗时：{} 毫秒 ======",takeTime);
        }
        return result;
    }
}
